﻿
Partial Class WebForm_CommonControl_MySlidingMenuGroup
    Inherits System.Web.UI.UserControl

    Protected Sub rep_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles rep.ItemCommand
        If e.CommandName = "lknMenuName" Then
            Response.Redirect(e.CommandArgument)
        End If
    End Sub

    Protected Sub rep_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rep.ItemDataBound
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            If DataBinder.Eval(e.Item.DataItem, "ProgramID") = Request("ProgramID") Then
                TryCast(e.Item.FindControl("aMenuName"), HtmlAnchor).Attributes.Add("style", "color:Red;")
            End If

            Dim url As String = ""
            If DataBinder.Eval(e.Item.DataItem, "Url").ToString().Contains("http") = True Then
                url = DataBinder.Eval(e.Item.DataItem, "Url").ToString()
            Else
                If DataBinder.Eval(e.Item.DataItem, "ProgramType").ToString().Contains("Public") = True Then
                    url = String.Format("~\{0}\{1}?ProgramID={2}", "Public", DataBinder.Eval(e.Item.DataItem, "Url"), DataBinder.Eval(e.Item.DataItem, "ProgramID"))
                Else
                    url = String.Format("~\{0}\{1}?ProgramID={2}", Me.ProgramDirectory, DataBinder.Eval(e.Item.DataItem, "Url"), DataBinder.Eval(e.Item.DataItem, "ProgramID"))
                End If
            End If

            '選單小圖
            If DataBinder.Eval(e.Item.DataItem, "img").ToString() <> "" Then
                'TryCast(e.Item.FindControl("ItemImage"), Image).ImageUrl = Me.ResolveUrl("~/Public/Downloads/Menu/" & DataBinder.Eval(e.Item.DataItem, "Img"))
            End If

            TryCast(e.Item.FindControl("aMenuName"), HtmlAnchor).HRef = Me.ResolveUrl(url)
            TryCast(e.Item.FindControl("aMenuName"), HtmlAnchor).Target = DataBinder.Eval(e.Item.DataItem, "target")
            TryCast(e.Item.FindControl("aMenuName"), HtmlAnchor).InnerText = DataBinder.Eval(e.Item.DataItem, "ProgramName")
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub
End Class

Partial Class WebForm_CommonControl_MySlidingMenuGroup
    Public Sub BindGV(ByVal dt As Data.DataTable)

        If Me.ListType = "L" Or Me.ListType = "" Then
            Me.rep.DataSource = dt
            Me.rep.DataBind()

            Me.mvList.SetActiveView(Me.vwList)
        ElseIf Me.ListType = "T" Then
            Me.AddNodes(Me.tv.Nodes, dt)
            Me.mvList.SetActiveView(Me.vwTree)
        End If
    End Sub

    Public Sub AddNodes(ByVal childNodes As TreeNodeCollection, ByVal dt As Data.DataTable)
        For Each dr As Data.DataRow In dt.Rows
            Dim node As New TreeNode(dr("ProgramName"))

            node.Expanded = False

            If Not TypeOf dr("Url") Is DBNull And dr("Url") <> "" Then
                If dr("Url").ToString().Contains("http") = True Then
                    node.NavigateUrl = dr("Url")
                    node.Target = "_blank"
                Else
                    node.NavigateUrl = String.Format("~\{0}\{1}?ProgramID={2}", Me.ProgramDirectory, dr("Url"), dr("ProgramID"))
                    node.Target = dr("target")

                    If dr("ProgramID") = Request("ProgramID") Then
                        node.Selected = True
                        node.Expanded = True
                    End If

                End If
            End If

            childNodes.Add(node)

            If node.Expanded.Value = True Then
                Dim parent As TreeNode = node.Parent

                While parent IsNot Nothing
                    parent.Expanded = True
                    parent = parent.Parent
                End While
            End If

            Dim e As New TreeViewNodeAddedEventArgs
            e.Node = node
            e.ProgramID = dr("ProgramID")

            RaiseEvent TreeViewNodeAdded(Me, e)
        Next
    End Sub

    Public Property ProgramID() As String
        Get
            Return ViewState("ProgramID")
        End Get
        Set(ByVal value As String)
            ViewState("ProgramID") = value
        End Set
    End Property

    Public Property MenuName() As String
        Get
            Return Me.lblMenuName.Text
        End Get
        Set(ByVal value As String)
            Me.lblMenuName.Text = value
        End Set
    End Property

    Public Property ProgramDirectory() As String
        Get
            If ViewState("ProgramDirectory") Is Nothing Then
                Return "WebForm"
            End If

            Return ViewState("ProgramDirectory")
        End Get
        Set(ByVal value As String)
            ViewState("ProgramDirectory") = value
        End Set
    End Property

    Public Property ListType() As String
        Get
            If ViewState("ListType") Is Nothing Then
                Return "L"
            End If

            Return ViewState("ListType")
        End Get
        Set(ByVal value As String)
            ViewState("ListType") = value
        End Set
    End Property

    Public Event TreeViewNodeAdded(ByVal source As WebForm_CommonControl_MySlidingMenuGroup, ByVal e As TreeViewNodeAddedEventArgs)

    Public Class TreeViewNodeAddedEventArgs
        Inherits EventArgs

        Public Node As TreeNode
        Public ProgramID As String
    End Class
End Class